package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatGameServerResponseParams;
import com.foolishbird.sdk.params.RedSendResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatGameServer;
import com.foolishbird.sdk.params.bean.RedConfig;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;


public class RedpackageSendFragment extends BaseHttpFragment implements View.OnClickListener {



    private TextView titleTxt;

    private ImageView backView,totalMoneyIcon;
    private TextView typeText,totoaMoneyText,moneyShowText;
    private TextView numberEdit,moneyEdit;
    private Button confirmBtn;
    private LinearLayout typeSelectLayout;
    private int type = 1;
    private String money;

    public static final RedpackageSendFragment newInstance(Bundle bundle) {
        RedpackageSendFragment fragment = new RedpackageSendFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    private RedConfig mConfig;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_redpackage_send");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("发红包");

        typeSelectLayout = contentView.findViewById(getViewIdentifier("redpackage_send_typelayout"));
        typeSelectLayout.setOnClickListener(this);
        typeText = contentView.findViewById(getViewIdentifier("redpackage_send_typetext"));

        numberEdit = contentView.findViewById(getViewIdentifier("redpackage_send_number"));
        numberEdit.setOnClickListener(this);
        totalMoneyIcon = contentView.findViewById(getViewIdentifier("redpackage_send_totalIcon"));
        totoaMoneyText = contentView.findViewById(getViewIdentifier("redpackage_send_totalMoney"));
        moneyEdit = contentView.findViewById(getViewIdentifier("redpackage_send_moneyEdit"));
        moneyEdit.setOnClickListener(this);
        moneyShowText = contentView.findViewById(getViewIdentifier("redpackage_send_moneyShow"));
        confirmBtn = contentView.findViewById(getViewIdentifier("redpackage_send_confirm"));
        confirmBtn.setOnClickListener(this);

        mConfig = (RedConfig)getArguments().getSerializable("config");

//        HttpEngineApi.getInstance().getFloatGameServer(requestCallbackHandler(FloatGameServerResponseParams.class));
        return contentView;

    }

//    private List<FloatGameServer> myNoticeList;

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof RedSendResponseParams){
                    RedSendResponseParams.RedSendResponseData data =((RedSendResponseParams)response).getData();
                    Bundle bunder = new Bundle();
                    bunder.putSerializable("data",data);
                    bunder.putString("money",money);
                    MyFragmentManger.getInstance().gotoFragment(RedpackageQRCodeFragment.newInstance(bunder));
                }
            }
//            setAdapter();
        }



        WL.d("FloatNoticeFragment",response.toString());
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if(!hidden){
//            MyFragmentManger.getInstance().goBack(this);
            Bundle bundle = new Bundle();

            MyFragmentManger.getInstance().gotoFragment(RedpackageGetFragment.newInstance(null));
            MyFragmentManger.getInstance().goBackOnly(this);
        }
    }

    @Override
    public void onClick(View view) {
        if(view ==backView){
            MyFragmentManger.getInstance().goBack(this);
        }else if (view ==confirmBtn){
            String number = numberEdit.getText().toString().trim();

            if(TextUtils.isEmpty(number)){
                Toast.makeText(getContext(),"请选择红包个数",Toast.LENGTH_SHORT).show();
                return;
            }
            if(TextUtils.isEmpty(money)){
                Toast.makeText(getContext(),"请选择红包金额",Toast.LENGTH_SHORT).show();
                return;
            }
            HttpEngineApi.getInstance().redpackageSend(money,number,type,requestCallbackHandler(RedSendResponseParams.class));
//            MyFragmentManger.getInstance().gotoFragment(RedpackageQRCodeFragment.newInstance(null));
        }else if(view == typeSelectLayout){
            ArrayList<String> aa = new ArrayList<>();
            aa.add("拼手气红包");
            aa.add("普通红包");

            Bundle bundle = new Bundle();
            bundle.putStringArrayList("list",aa);
            RedpackageChooseFragment fragment = RedpackageChooseFragment.newInstance(bundle);
            fragment.setmListener(new RedpackageChooseFragment.OnCommStringChoiceListener() {
                @Override
                public void onCommonStringChoice(String string) {
                    typeText.setText(string);
                    type = "拼手气红包".equals(string)?1:2;
//                    moneyEdit.setText("");
//                    moneyShowText.setText("");
//                    money= "";
//                    if("拼手气红包".equals(string)){
//                        totalMoneyIcon.setVisibility(View.VISIBLE);
//                        totoaMoneyText.setText("总金额");
//                        if(type!=1){
//                            type=1;
//                        }
//                    }else {
//                        totalMoneyIcon.setVisibility(View.GONE);
//                        totoaMoneyText.setText("单个金额");
//                        if(type!=2){
//                            type=2;
//                        }
//                    }
                }
            });
            MyFragmentManger.getInstance().gotoFragment(fragment);
        }else if (view ==moneyEdit){

            Bundle bundle = new Bundle();
            ArrayList list = new ArrayList<>();
            Collections.addAll(list,mConfig.getAmountOption());
            bundle.putStringArrayList("list", list);
            bundle.putString("color","#E63624");
            RedpackageChooseFragment fragment = RedpackageChooseFragment.newInstance(bundle);
            fragment.setmListener(new RedpackageChooseFragment.OnCommStringChoiceListener() {
                @Override
                public void onCommonStringChoice(String string) {
                    money = string;
                    moneyEdit.setText("￥"+string);
                    moneyShowText.setText("￥"+string);
//                    if(type==1){
//                        moneyShowText.setText("￥"+string);
//                    }else {
//                        moneyShowText.setText("￥"+Integer.valueOf(number)*Integer.valueOf(string));
//                    }
                }
            });
            MyFragmentManger.getInstance().gotoFragment(fragment);
        }else if (view ==numberEdit){
            Bundle bundle = new Bundle();
            ArrayList list = new ArrayList<>();
            Collections.addAll(list,mConfig.getCountOption());
            bundle.putStringArrayList("list", list);
            RedpackageChooseFragment fragment = RedpackageChooseFragment.newInstance(bundle);
            fragment.setmListener(new RedpackageChooseFragment.OnCommStringChoiceListener() {
                @Override
                public void onCommonStringChoice(String string) {
                    numberEdit.setText(string);
                }
            });
            MyFragmentManger.getInstance().gotoFragment(fragment);
        }
//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }



}
